﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScheduleMaker.DB.Domain;
using ScheduleMaker.DB.Domain.Core;

namespace ScheduleMaker.DB.CoreDAO
{
    public class SubjectDAO : CoreAbstractDAO<Subject>
    {
        private AdapterAbstractDAO<Subject, SubjectSchool> wrappedAbstractDAOSchool;
        private AdapterAbstractDAO<Subject, SubjectUniversity> wrappedAbstractDAOUniversity;
        private SubjectDAOSchool wrappedDAOSchool;
        private SubjectDAOUniversity wrappedDAOUniversity;

        public SubjectDAO(SubjectDAOSchool wrappedDAOSchool)
        {
            this.wrappedDAOSchool = wrappedDAOSchool;
            this.wrappedAbstractDAOSchool =
                new AdapterAbstractDAO<Subject, SubjectSchool>((AbstractDAO<SubjectSchool>)wrappedDAOSchool);
        }

        public SubjectDAO(SubjectDAOUniversity wrappedDAOUniversity)
        {
            this.wrappedDAOUniversity = wrappedDAOUniversity;
            this.wrappedAbstractDAOUniversity =
                new AdapterAbstractDAO<Subject, SubjectUniversity>((AbstractDAO<SubjectUniversity>)wrappedDAOUniversity);
        }

        public IList<Subject> getSubjectsByClassroom(Classroom classroom)
        {
            if (wrappedDAOSchool != null)
            {
                List<SubjectSchool> list = (List<SubjectSchool>)wrappedDAOSchool.getSubjectsByClassroom(classroom);
                return list.ConvertAll(x => (Subject)x);
            }
            else if (wrappedDAOUniversity != null)
            {
                List<SubjectUniversity> list = (List<SubjectUniversity>)wrappedDAOUniversity.getSubjectsByClassroom(classroom);
                return list.ConvertAll(x => (Subject)x);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped SubjectDAOSchool nor SubjectDAOUniversity was initialized");
            }
        }

        public IList<Subject> getSubjectsByCategory(Category category)
        {
            if (wrappedDAOSchool != null)
            {
                throw new NotSupportedMethodException();
            }
            else if (wrappedDAOUniversity != null)
            {
                List<SubjectUniversity> list = (List<SubjectUniversity>)wrappedDAOUniversity.getSubjectsByCategory(category);
                return list.ConvertAll(x => (Subject)x);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped SubjectDAOSchool nor SubjectDAOUniversity was initialized");
            }
        }

        public override void update(Subject entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.update(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.update(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped SubjectDAOSchool nor SubjectDAOUniversity was initialized");
            }
        }

        public override void save(Subject entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.save(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.save(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped SubjectDAOSchool nor SubjectDAOUniversity was initialized");
            }
        }

        public override void saveOrUpdate(Subject entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.saveOrUpdate(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.saveOrUpdate(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped SubjectDAOSchool nor SubjectDAOUniversity was initialized");
            }
        }

        public override void delete(Subject entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.delete(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.delete(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped SubjectDAOSchool nor SubjectDAOUniversity was initialized");
            }
        }

        public override Subject getById(int id)
        {
            Subject entity;
            if (wrappedDAOSchool != null)
            {
                entity = wrappedAbstractDAOSchool.getById(id);
            }
            else if (wrappedDAOUniversity != null)
            {
                entity = wrappedAbstractDAOUniversity.getById(id);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped SubjectDAOSchool nor SubjectDAOUniversity was initialized");
            }

            return entity;
        }

        public override IList<Subject> getAll()
        {
            IList<Subject> records;
            if (wrappedDAOSchool != null)
            {
                records = wrappedAbstractDAOSchool.getAll();
            }
            else if (wrappedDAOUniversity != null)
            {
                records = wrappedAbstractDAOUniversity.getAll();
            }
            else
            {
                throw new NotImplementedException("Neither wrapped SubjectDAOSchool nor SubjectDAOUniversity was initialized");
            }

            return records;
        }
    }
}
